package com.sunfield.windpower.decoration;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;

import com.sunfield.baseframe.util.ScreenUtils;
import com.sunfield.windpower.R;
import com.sunfield.windpower.adapter.QuickEnterAdapter;


/**
 * Created by wangxl on 2016/11/15.
 */
public class ApproveOnlineDecoration extends RecyclerView.ItemDecoration {
    QuickEnterAdapter adapter;
    private Paint mPaint;
    private Rect mBounds;
    private int mTitleHeight;
    private int textColor;
    private int paddingLeft;
    private int colorBg;

    public ApproveOnlineDecoration(QuickEnterAdapter adapter) {
        this.adapter = adapter;

        mPaint = new Paint();
        mPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 15, adapter.getContext().getResources().getDisplayMetrics()));
        mPaint.setAntiAlias(true);
        mBounds = new Rect();

        mTitleHeight = ScreenUtils.dip2px(adapter.getContext(), 45);
        paddingLeft = ScreenUtils.dip2px(adapter.getContext(), 16);
        textColor = adapter.getContext().getResources().getColor(R.color.text_color_light_gray);
        colorBg = Color.WHITE;
    }

    int getItemType(int position) {
        if (position == 0 && adapter.getType1Count() > 0) {
            return 1;
        }
        if (position == adapter.getType1Count() && adapter.getType2Count() > 0) {
            return 2;
        }
        return 0;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

        int childCount = parent.getChildCount();
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            int position = params.getViewLayoutPosition();
            switch (getItemType(position)) {
                case 1:
                    drawSection("我的流程", c, left, right, child, params, position);
                    break;
                case 2:
                    drawSection("我的任务", c, left, right, child, params, position);
                    break;
            }
        }
    }

    private void drawSection(String text, Canvas c, int left, int right, View child, RecyclerView.LayoutParams params, int position) {
        mPaint.setColor(colorBg);
        c.drawRect(left, child.getTop() - params.topMargin - mTitleHeight, right, child.getTop() - params.topMargin, mPaint);

        mPaint.setColor(textColor);
        mPaint.getTextBounds(text, 0, text.length(), mBounds);
        c.drawText(text, paddingLeft, child.getTop() - params.topMargin - (mTitleHeight / 2 - mBounds.height() / 2), mPaint);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        if (getItemType(position) == 0) {
            outRect.set(0, 0, 0, 0);
        } else {
            outRect.set(0, mTitleHeight, 0, 0);
        }
    }

    @Override
    public void onDrawOver(Canvas c, final RecyclerView parent, RecyclerView.State state) {
        int pos = ((LinearLayoutManager) (parent.getLayoutManager())).findFirstVisibleItemPosition();
        String msg = "";
        if (adapter.getItemCount() == 0) {
            return;
        }
        if (pos < adapter.getType1Count()) {
            msg = "我的流程";
        } else {
            msg = "我的任务";
        }

        View child = parent.findViewHolderForLayoutPosition(pos).itemView;
        boolean flag = false;//定义一个flag，Canvas是否位移过的标志
        if (adapter.getType1Count() > 0 && pos == adapter.getType2Count() - 1) {
            if (child.getHeight() + child.getTop() < mTitleHeight) {//当第一个可见的item在屏幕中还剩的高度小于title区域的高度时，我们也该开始做悬浮Title的“交换动画”
                c.save();//每次绘制前 保存当前Canvas状态，
                flag = true;
                c.translate(0, child.getHeight() + child.getTop() - mTitleHeight);
            }
        }

        mPaint.setColor(colorBg);
        c.drawRect(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getRight() - parent.getPaddingRight(), parent.getPaddingTop() + mTitleHeight, mPaint);
        mPaint.setColor(textColor);
        mPaint.getTextBounds(msg, 0, msg.length(), mBounds);
        c.drawText(msg, paddingLeft, parent.getPaddingTop() + mTitleHeight - (mTitleHeight / 2 - mBounds.height() / 2), mPaint);

        if (flag)
            c.restore();//恢复画布到之前保存的状态
    }
}
